/**
 * 付款类型详情
 */
import React, { Component } from 'react';
import { connect } from 'react-redux'
import { messages, deepFullCopy } from "share/common";
import { Form, Input } from 'antd'
import InputLanguage from 'components/template/input-language'
import SlideFrameContent from '../slide-frame-content'
import settingService from '../request-and-expense-settings.service'
import 'styles/request-and-expense-settings/expense-setting.scss'

class ExpensePayTypeDetail extends Component {

  record = this.props.params.record || {};

  priorityDefaultValue = this.props.authPolicyAll ? {
    levelCode: this.props.tenant.tenantFormRecommend ? 'TENANT' : 'SET_OF_BOOK',
    levelOrgId: this.props.tenant.tenantFormRecommend ? this.props.tenant.id : this.props.company.setOfBooksId,
    levelOrgName: this.props.tenant.tenantFormRecommend ? this.props.tenant.tenantName : this.props.company.setOfBooksName,
  } : undefined;

  priorityValue = {
    levelCode: this.record.levelCode,
    levelOrgId: this.record.levelOrgId || this.record.setOfBookId,
    levelOrgName: this.record.levelOrgName || this.record.setOfBookName,
  };

  priorityInfo = {
    top: this.record.levelCode || this.props.authPolicyAll ? 'TENANT' : 'SET_OF_BOOK',
    disabled: !!this.record.id,
    value: this.record.id ? this.priorityValue : this.priorityDefaultValue,
    extraParams: {
      showSetOfBooks: true,
      showCompany: false,
      showCompanyComponent: false, // 租户和帐套层级都不可分配公司
    }
  };

  formList = [{
    id: 'classCode',
    label: messages('expense-7.key35')/*付款类型代码*/,
    rules: [{
      required: true,
      message: messages('expense-7.key36')/*请输入temp2*/
    }],
    initialValue: this.record.classCode,
    component: <Input placeholder={messages('expense-7.key36')/*请输入temp2*/} maxLength={200} disabled={!!this.record.id}/>
  }, {
    id: 'description',
    label: messages('expense-7.key37')/*付款类型名称*/,
    rules: [{
      required: true,
      message: messages('expense-7.key36')/*请输入temp2*/
    }, {
      validator: (item, value, callback) => {
        if (value && !value.name) {
          callback(messages('expense-7.key36')/*请输入temp2*/)
        }
        callback();
      }
    }],
    initialValue: this.record.id ? {
      name: this.record.description,
      i18n: this.record.i18n.description || [
        {language: "zh_cn", value: this.record.description},
        {language: "en", value: this.record.description}
      ]
    } : undefined,
    component: <InputLanguage maxLength={200} placeholder={messages('expense-7.key36')/*请输入temp2*/} disabled={!this.props.authPolicyAll}/>
  }];

  dealParams = values => {
    let params = deepFullCopy(this.record);
    if (values.levelCode === "SET_OF_BOOK") {
      params.setOfBookId = values.levelOrgId;
    }
    if (values.description) {
      values.i18n = params.i18n || {};
      values.i18n.description = values.description.i18n;
      values.description = values.description.name;
    }
    values.tenantId = params.tenantId || this.props.tenant.id;
    values.typeCode = "PAYMENT";
    Object.keys(values).map(key => {
      if (!['levelCode', 'levelOrgId', 'companies'].includes(key)) {
        params[key] = values[key]
      }
    });
    return params;
  };

  render() {
    const { authPolicyAll } = this.props;
    return (
      <SlideFrameContent
        enabledKey="enabled"
        enabled={this.record.id ? this.record.enabled : true}
        disabled={!authPolicyAll}
        priorityInfo={this.priorityInfo}
        formInfo={[{list: this.formList}]}
        service={this.record.id ? settingService.updatePayType : settingService.newPayType}
        wrappedComponentRef={inst => this.formRef = inst}
        dealParams={this.dealParams}
        close={this.props.close}
      />
    )
  }

}

function mapStateToProps(state) {
  return {
    tenant: state.login.tenant,
    company: state.login.company,
    authPolicyAll: state.main.authPolicyAll, // 是否为全租户权限
    currentMenuPermission: state.main.currentMenuPermission,
  }
}

const WrappedExpensePayTypeDetail = Form.create()(ExpensePayTypeDetail);

export default connect(mapStateToProps)(WrappedExpensePayTypeDetail);
